<template>
  <page-header-wrapper :title="false">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <!-- <a-col :span="8">
              <a-form-item label="联系电话">
                <a-input v-model="queryParam.contactNumber" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :span="8">
              <a-form-item label="计酬方式">
                <a-select v-model="queryParam.taskUnit" placeholder="请选择" default-value="">
                  <a-select-option value="0">0天</a-select-option>
                  <a-select-option value="1">1次</a-select-option>
                  <a-select-option value="2">2月</a-select-option>
                  <a-select-option value="3">3年</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :offset="16" :span="8" style="text-align: right;">
              <span class="table-page-search-submitButtons">
                <a-button icon="plus" style="margin-right: 8px" type="primary" @click="handleAdd">新建</a-button>
                <!-- <a-button type="primary" @click="handleExport">导出</a-button> -->
                <!-- <a-button type="primary" style="margin-left: 8px" @click="handleSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :pageNum="pageNum"
        :pageSize="pageSize"
        rowKey="id"
        size="default">
        <div slot="action" slot-scope="text, record">
          <a :disabled="record.state === 1" type="primary" @click="handleStatus(record)">激活</a>
          <a :disabled="record.state === 2" style="margin-left:10px;" @click="handleStatus(record)">冻结</a>
          <a style="margin-left:10px;" type="primary" @click="handleEdit(record)">更新</a>
          <a style="margin-left:10px;" type="primary" @click="handlePreview(record)">预览</a>
        </div>
        <!-- <div slot="content" slot-scope="text, record">
          <div v-html="record.content"></div>
        </div> -->
      </s-table>
    </a-card>
    <CreateForm
      ref="createModal"
      :loading="confirmLoading"
      :model="mdl"
      :visible="visible"
      @cancel="handleCancel"
      @ok="handleOk" />

    <Preview ref="createModal" :model="mdl" :visible="showPreview" @cancel="showPreview = false" />

  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import {
  active,
  frozen,
  knowledgeAdd,
  knowledgeCategoryList as categoryList,
  knowledgeDetail,
  knowledgeUpdate,
  list
} from '@/api/knowledge'
import { knowledgeTypeExport } from '@/api/export'
import CreateForm from './modules/manage-form'
import Preview from './modules/Preview'

export default {
  name: 'TableList',
  components: {
    STable,
    CreateForm,
    Preview
  },
  data () {
    return {
      data: [],
      queryParam: {},
      pageNum: 1,
      pageSize: 20,
      confirmLoading: false,
      visible: false,
      showPreview: false,
      mdl: null,
      categoryList: [],
      columns: [
        // {
        //   title: '内容描述',
        //   dataIndex: 'content',
        //   scopedSlots: { customRender: 'content' }
        // },
        {
          title: '类别',
          dataIndex: 'typeName'
        },
        {
          title: '分类',
          dataIndex: 'categoryName'
        }, {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '作者',
          dataIndex: 'author'
        },
        {
          title: '摘要',
          dataIndex: 'brief'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime'
        },
        // {
        //   title: '文件',
        //   dataIndex: 'files',
        //   customRender: (arr) => {
        //     return arr.map(i => {
        //       return <div><span>{i.fileUrl}</span><span>{i.fileType}</span></div>
        //     })
        //   }
        // },
        {
          title: '操作',
          width: 180,
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  },
  created () {
    this.getCategory()
  },
  methods: {
    handleSearch (e) {
      this.$refs.table.refresh()
    },
    async getCategory () {
      const res = await categoryList({ limit: 10000 })
      if (!res.success) return
      this.categoryList = res.data.records
    },
    dictFilter (val) {
      const item = this.categoryList.find(i => i.id === val)
      return item && item.name || ''
    },
    handleExport () {
      if (!this.$refs.table.localDataSource.length) {
        this.$message.warning('列表为空，不能导出')
        return
      }
      knowledgeTypeExport(this.queryParam)
    },
    handleAdd () {
      this.mdl = null
      this.$refs.createModal.coverFileList = []
      this.$refs.createModal.content = ''
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleReset () {
      this.queryParam = {}
      this.$refs.table.refresh()
    },
    loadData (params) {
      return list(Object.assign(params, this.queryParam)).then(res => {
        return res.data
      })
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          if (values.id > 0) {
            knowledgeUpdate(values).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('修改成功')
            }).catch(err => {
              this.confirmLoading = false
            })
          } else {
            // 新增
            knowledgeAdd(values).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('新增成功')
            }).catch(err => {
              this.confirmLoading = false
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.$refs.createModal.coverFileList = []
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleStatus (row) {
      const { state, id } = row
      this.$confirm({
        title: `您确定要${state === 1 ? '冻结' : '激活'}吗？`,
        onOk: () => {
          if (row.state === 1) {
            frozen([id]).then(() => {
              this.$message.success('冻结成功')
              this.$refs.table.refresh()
            })
          } else {
            active([id]).then(() => {
              this.$message.success('激活成功')
              this.$refs.table.refresh()
            })
          }
        }
      })
    },

    handlePreview (record) {
      knowledgeDetail(record.id).then(res => {
        this.showPreview = true
        this.mdl = res.data
      }).catch(err => {
        this.showPreview = false
      })
      // this.showPreview = true
      // this.mdl = { ...record }
    }
  }
}
</script>
